package com.jtjrenren.android.taxi.driver.core.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jtjrenren.android.taxi.driver.base.BaseRcyAdapter;
import com.jtjrenren.android.taxi.driver.bean.OrderDesc;
import com.jtjrenren.android.taxi.driver.bean.OrderQD;
import com.jtjrenren.android.taxi.driver.utils.GsonUtils;
import com.jtjrenren.android.taxi.driver.R;
import com.library.utils.DensityUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/7/6.
 */
public class JdListAdapter extends BaseRcyAdapter<OrderQD> {
    private clickListener clickListener;

    public void setClickListener(JdListAdapter.clickListener clickListener) {
        this.clickListener = clickListener;
    }

    public JdListAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_jd_list, parent, false);
        return new JDHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderQD orderQD = list.get(position);

        JDHolder jd = (JDHolder) holder;
        String desc=orderQD.getXdResponse().getOrderAddrDesc();
//        String[] strings = ModuleUtils.parseXdDesc(orderQD.getXdResponse().getOrderAddrDesc());

        OrderDesc orderDesc= GsonUtils.getObj(desc,OrderDesc.class);
        if (orderDesc==null)
            return;

        String reg = "[0-9]+([.]{1}[0-9]+){0,1}";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(orderDesc.getDT());
        String str = "";
        if (matcher.find()) {
            str = matcher.group(0);
        }

        String strs = orderDesc.getDT().replace("乘客", "");
        SpannableStringBuilder sp = new SpannableStringBuilder(strs);
        int start = strs.indexOf(str);
        int end = start + str.length();
        sp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.label_orange)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        sp.setSpan(new AbsoluteSizeSpan(DensityUtils.sp2px(context,18)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        jd.tv_desc.setText(sp);
        jd.tv_start.setText(orderDesc.getFA());
        jd.tv_end.setText(orderDesc.getTA());
    }

    public class JDHolder extends RecyclerView.ViewHolder {
        private TextView tv_start, tv_end, tv_desc;

        public JDHolder(View itemView) {
            super(itemView);

            tv_start = (TextView) itemView.findViewById(R.id.item_jd_list_start);
            tv_end = (TextView) itemView.findViewById(R.id.item_jd_list_end);
            tv_desc = (TextView) itemView.findViewById(R.id.item_jd_list_desc);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onItemClick(list.get(getAdapterPosition()));
                }
            });
        }
    }

    public interface clickListener {
        void onItemClick(OrderQD qd);
    }
}
